﻿using System.ComponentModel.Composition;
using Caliburn.Micro;
using SS.WPF.Framework.ViewModel.Screen;

namespace SS.WPF.Framework.BusyIndicator.ViewModels
{
    /// <summary>
    /// Present the ViewModel displaying as an Busy Indicator
    /// </summary>
    [Export(typeof(BusyIndicatorViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class BusyIndicatorViewModel : DialogViewModelBase
    {
        /// <summary>
        /// The _lock object.
        /// </summary>
        private readonly object _lockObject = new object();

        /// <summary>
        /// Gets a value indicating whether IsShowed.
        /// </summary>
        public bool IsShowed { get; private set; }

        private string _title = "Loading, please wait...";
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                NotifyOfPropertyChange(() => this.Title);
            }
        }

        /// <summary>
        /// The on showed.
        /// </summary>
        public void LoadedData()
        {
            lock (this._lockObject)
            {
                this.IsShowed = true;
            }
        }

        /// <summary>
        /// The on stopped.
        /// </summary>
        public void OnStopped()
        {
            lock (this._lockObject)
            {
                if (this.IsShowed)
                {
                    this.IsShowed = false;
                    this.SetDialogResult(true);
                }
            }
        }
    }
}
